<?php

namespace App\Repositories;

use Prettus\Repository\Eloquent\BaseRepository;
use Prettus\Repository\Criteria\RequestCriteria;
use App\Repositories\PermissionRepository;
use App\Models\Permission;
use Illuminate\Support\Facades\Route;

/**
 * Class PermissionRepositoryEloquent
 * @package namespace App\Repositories;
 */
class PermissionRepositoryEloquent extends BaseRepository implements PermissionRepository
{
    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Permission::class;
    }

    

    /**
     * Boot up the repository, pushing criteria
     */
    public function boot()
    {
        $this->pushCriteria(app(RequestCriteria::class));
    }

    /**
     * get top permissions
     * @return mixed
     */
    public function getTopPermission()
    {
        return $this->model->where([
            ['namespace', '!=', ''],
            ['controller', ''],
            ['action', '']
        ])->orderBy('sort', 'asc')->orderBy('id', 'asc')->get();
    }

    public function getMenu()
    {
        $curr_user = request()->user(); // 当前登录用户
        $path = Route::currentRouteName();
        // 获取主菜单
        $menus = $this->model->where([
            ['status', 1],
            ['namespace', '!=', ''],
            ['controller', ''],
            ['action', ''],
        ])->orderBy('sort','asc')->get(['id','namespace', 'controller', 'action','name', 'icon']);

        $curr_menus = [];
        foreach ($menus as $key => $item)
        {
            // 判断主菜单权限
            if ( !IS_ROOT && !$curr_user->can($item->namespace.'.'.$item->controller.'.'.$item->action))  continue;
            // 高亮主菜单
            $curr_menus[$key] = [
                'id' => $item->id,
                'namespace' => $item->namespace,
                'controller' => $item->controller,
                'action' => $item->action,
                'name' => $item->name,
                'icon' => $item->icon,
            ];
            if($path  == strtolower($item->namespace.'/'.$item->controller.'/'.$item->action))
            {
                $curr_menus[$key]['class'] = 'active';
            }
            // 查找当前子菜单
            $childs = $this->model->where([
                ['status', 1],
                ['id', '!=', $item['id']],
                ['namespace', $item['namespace']]
            ])->get(['id','namespace', 'controller', 'action', 'name', 'icon']);
            foreach ($childs as $child)
            {
                if ( !IS_ROOT && !$curr_user->can($child->namespace.'.'.$child->controller.'.'.$child->action))  continue;
                $curr_menus[$key]['child'][] = [
                    'id' => $child->id,
                    'namespace' => $child->namespace,
                    'controller' => $child->controller,
                    'action' => $child->action,
                    'name' => $child->name,
                    'icon' => $child->icon,
                ];
            }
        }
        return $curr_menus;
    }
}
